<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of NotificationController
 *
 * @author Admin
 */
class Marketing_MyhousesController extends Zend_Controller_Action {

    public function init() {
        /* Initialize action controller here */
    }

    public function preDispatch() {
        $auth = Zend_Auth::getInstance();
        if (!$auth->hasIdentity()) {
            $this->_redirect('/user/login');
        }

        $view = new Zend_View();
        $view->headTitle(' > Marketing > House');
        $this->view->userDetail = $auth->getIdentity();
    }

    public function indexAction() {
        $houseModel = new Application_Model_HouseModel();
        $paginator = $houseModel->getAllUserAssignedSwitcheHousesForMarketing($this->_request, $this->view->userDetail->id);
        $this->view->paginator = $paginator;
    }

    public function listAction() {
        $this->_helper->layout->disableLayout();
        $houseModel = new Application_Model_HouseModel();
        $paginator = $houseModel->getAllUserAssignedSwitcheHousesForMarketing($this->_request, $this->view->userDetail->id);
        $this->view->paginator = $paginator;
    }

    public function addAction() {
        $this->_helper->layout->disableLayout();
        $form = new Application_Form_house_FilesForm();
        $this->view->form = $form;
        $switchId = $this->_request->getParam('id');
        $documentModel = new Application_Model_DocumentModel();
        if ($this->_request->isPost()) {
            $formData = $this->_request->getPost();
            if ($form->isValid($formData)) {
                $uploaded = move_uploaded_file($_FILES["doc_path"]["tmp_name"], "./uploads/files/" . $_FILES["doc_path"]["name"]);
                if ($uploaded) {
                    $data = array(
                        'name' => $this->_request->getPost('name'),
                        'file' => $_FILES["doc_path"]["name"],
                        'uploader_id'=>  $this->view->userDetail->id,
                        'switch' => $this->_request->getPost('switch'),
                        'status' => 'active',
                        'date' => Zend_Date::now()->toString('yyyy-MM-dd HH:mm:ss')
                    );                    
                    $documentModel->save($data);
                    $this->_redirect("marketing/myhouses");
                    exit;
                }
            }
        }
        $docs = $documentModel->getAllUserSwitchDocument($switchId,$this->view->userDetail->id);
        $this->view->files = $docs;
        $this->view->switchId = $switchId;
    }

    public function interactionAction() {
        $this->_helper->layout->disableLayout();
        $houseModel = new Application_Model_HouseModel();

        $id = $this->_request->getParam('id');
        $interaction = $houseModel->getHouseDetail($id);
        $this->view->interaction = $interaction;
        $this->view->houseId = $id;
    }

    public function updateinteractionAction() {
        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);

        $houseModel = new Application_Model_HouseModel();

        $id = $this->_request->getParam('id');
        $interaction = $this->_request->getPost('interactionNo');
        $value = $this->_request->getPost('value');

        $houseModel->update(array("$interaction" => $value), "id = '$id'");
    }

    public function contractAction() {
        $this->_helper->layout->disableLayout();
        $form = new Application_Form_documents_AddForm();
        $documentModel = new Application_Model_DocumentModel();
        $this->view->form = $form;
        $houseId = $this->_request->getParam('id');

        if ($this->_request->isPost()) {

            $formData = $this->_request->getPost();
            if ($form->isValid($formData)) {
                $uploaded = move_uploaded_file($_FILES["doc_path"]["tmp_name"], "./uploads/files/" . $_FILES["doc_path"]["name"]);
                if ($uploaded) {
                    $data = array(
                        'name' => $this->_request->getPost('name'),
                        'file' => $_FILES["doc_path"]["name"],
                        'marketing_house_id' => $this->_request->getPost('houseId'),
                        'uploader_id' => $this->view->userDetail->id,
                        'status' => 'active',
                        'date' => Zend_Date::now()->toString('yyyy-MM-dd HH:mm:ss')
                    );
                    if ($this->_request->getPost('docId')) {
                        $data['id'] = $this->_request->getPost('docId');
                    }
                    $documentModel->save($data);
                    $this->_redirect("marketing/myhouses");
                    exit;
                }
            }
        }
        $this->view->houseId = $houseId;
    }

}